package com.cloudkinto.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.TemuOrderBackDao;
import com.cloudkinto.entity.TemuOrderBackDo;
import com.cloudkinto.service.order.TemuOrderBackService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author hua
 * @since 2024-09-13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TemuOrderBackServiceImpl extends ServiceImpl<TemuOrderBackDao, TemuOrderBackDo> implements TemuOrderBackService {

    @Override
    public Object detail(Long id) {
        TemuOrderBackDo entityDo = baseMapper.selectById(id);
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    /**
     * 查询明细之后要执行的业务定制处理
     */
    private Object handlerDetailAfter(Object res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        return SingleResult.success("");
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<TemuOrderBackDo> wrapper = queryBuild(map);
        IPage<TemuOrderBackDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<Object> resList = handlerListAfter(page.getRecords());
        return new PageResult(page.getRecords(), (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
    }

    /**
     * 查询分页列表之后对列表数据的处理
     */
    private List<Object> handlerListAfter(List<TemuOrderBackDo> list) {
        List<Object> result = new ArrayList<>();
        return result;
    }

    private IPage<TemuOrderBackDo> pageInit(Map<String, String> map) {
        IPage<TemuOrderBackDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<TemuOrderBackDo> queryBuild(Map<String, String> map) {
        QueryWrapper<TemuOrderBackDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.like("keyword", map.get("keyword"));
        }
        return wrapper;
    }


}
